<template>
  <div class="container" @scroll="remPos" ref="box">
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="detail">
      <h3 class="title">{{ detail.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="detail.aut_photo"
        />
        <div class="text">
          <p class="name">{{ detail.aut_name }}</p>
          <p class="time">{{ detail.pubdate | relTime }}</p>
        </div>
        <van-button @click="toggleFollow" round size="small" type="info">
          {{ detail.is_followed ? "已关注" : "+ 关注" }}
        </van-button>
      </div>
      <div class="content" v-html="detail.content"></div>
      <!-- 用户对文章的态度 =》attitude -1: 无态度，0-不喜欢，1-点赞（喜欢） -->
      <div class="zan">
        <van-button
          round
          size="small"
          :class="{ active: detail.attitude === 1 }"
          plain
          @click="toggleStatus(1)"
          icon="like-o"
          >点赞</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          @click="toggleStatus(0)"
          size="small"
          :class="{ active: detail.attitude === 0 }"
          plain
          icon="delete"
          >不喜欢</van-button
        >
      </div>
      <!-- 文章评论列表 -->
      <Comment />
    </div>
  </div>
</template>

<script>
/**
 * 记录用户阅读文章滚动位置=》被缓存文章（1）
 * 1. 通过scroll滚动事件存储滚动位置
 * 2. 下次再回来回到上次阅读位置
 */
import { getDetailById } from '@/api/detail.js'
import { follow, unFollow, like, unLike, disLike, unDisLike } from '@/api/user.js'
import Comment from './components/comment.vue'
export default {
  components: {
    Comment
  },
  name: 'articleDetail',
  data () {
    return {
      detail: {},
      scrollTop: 0 // 滚动位置
    }
  },
  // 组件缓存后会被执行：1. 默认第一次组件创建也会执行 2. 再次进入还会再执行
  activated () {
    // 当前访问文章详情ID和上次访问的文章ID不一样
    if (this.detail.art_id !== this.$route.params.id) {
      // 重置滚动位置
      this.scrollTop = 0
      this.getDetail()
    } else {
      // 执行滚动（阅读记忆）
      this.scrollTop && (this.$refs.box.scrollTop = this.scrollTop)
    }
  },
  // created () {
  //   this.getDetail()
  // },
  methods: {
    // 记录滚动位置
    remPos (e) {
      this.scrollTop = e.target.scrollTop
    },
    // 获取文章详情
    async getDetail () {
      const { data } = await getDetailById(this.$route.params.id)
      console.log(data)
      this.detail = data
    },
    // 关注文章作者或取关
    async toggleFollow () {
      /**
       * 思路：
       * 1. 如果关注过=》取关
       * 2. 如果没关注过=》关注
       * 数据库+前端修改
       */
      try {
        if (this.detail.is_followed) {
          // 取关
          await unFollow(this.detail.aut_id)
          this.detail.is_followed = false
        } else {
          // 关注
          await follow(this.detail.aut_id)
          this.detail.is_followed = true
        }
      } catch (error) {
        this.$toast.fail('操作失败')
      }
    },
    // 对文章表态：attitude -1: 无态度，0-不喜欢，1-点赞（喜欢）=> 3选1
    // type=> 1 点击了点赞按钮 0 点击了不喜欢按钮
    async toggleStatus (type) {
      try {
        if (type === 1) {
          // 写点赞逻辑
          if (this.detail.attitude === 1) {
            // 取消点赞
            await unLike(this.detail.art_id)
            this.detail.attitude = -1
          } else {
            // 点赞
            await like(this.detail.art_id)
            this.detail.attitude = 1
          }
        } else {
          // 写不喜欢逻辑
          if (this.detail.attitude === 0) {
            // 取消不喜欢
            await unDisLike(this.detail.art_id)
            this.detail.attitude = -1
          } else {
            // 不喜欢
            await disLike(this.detail.art_id)
            this.detail.attitude = 0
          }
        }
      } catch (error) {
        console.dir(error)
        this.$toast.fail('操作失败')
      }
    }
  }

}
</script>

<style scoped lang='less'>
.container {
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.detail {
  padding: 46px 10px 44px;
  // height: 1000%;
  .title {
    font-size: 18px;
    line-height: 2;
  }
  .zan {
    text-align: center;
    padding: 10px 0;
    .active {
      border-color: red;
      color: red;
    }
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.5;
      text-align: left;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  // 内容中包含：img 特别宽  code pre 不能换行
  .content {
    padding: 20px 0;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
    /deep/ code {
      white-space: pre-wrap;
    }
    /deep/ pre {
      white-space: pre-wrap;
    }
  }
}
</style>
